package lt.egidijus.aatest.wizards;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.concurrent.Semaphore;

import lt.egidijus.aatest.filters.AbstractClassFilter;
import lt.egidijus.aatest.filters.ConstructorFilter;
import lt.egidijus.aatest.filters.EnumFilter;
import lt.egidijus.aatest.filters.InterfaceFilter;
import lt.egidijus.aatest.generator.TestSuiteGenerator;
import lt.egidijus.aatest.generator.rules.Rule;
import lt.egidijus.aatest.model.AAtestClass;
import lt.egidijus.aatest.model.AAtestList;
import lt.egidijus.aatest.runner.AATestJunitLauncher;
import lt.egidijus.aatest.wizards.pages.AbstractClassesPage;
import lt.egidijus.aatest.wizards.pages.ConstantsPage;
import lt.egidijus.aatest.wizards.pages.ConstructorsPage;
import lt.egidijus.aatest.wizards.pages.EnumsPage;
import lt.egidijus.aatest.wizards.pages.InterfacesPage;
import lt.egidijus.aatest.wizards.pages.RulesPage;
import lt.egidijus.aatest.wizards.pages.TestsListPage;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;

public class RunCustomTestsWizard  extends Wizard {
	     TestsListPage testChoosePage;
	     protected AAtestList tList = null;

	 	public ISelection selection;
	 	Shell shell;

	 	public List<AAtestClass> selectedClassList = null;
	 	/**
	 	 * Constructor for GenerateTestWizard.
	 	 */
	 	public RunCustomTestsWizard(Shell shell, AAtestList tlist) {
	 		super();
	 		setWindowTitle("Generate Automatic tests");
	 		setNeedsProgressMonitor(true);
	 		this.shell = shell;
	 		this.tList = tlist;
	 	}

	 	/**
	 	 * Adding the page to the wizard.
	 	 */

	 	public void addPages() {

	 		testChoosePage = new TestsListPage(selection, shell, tList);
	 		addPage(testChoosePage);
	 	}

	 	/**
	 	 * This method is called when 'Finish' button is pressed in the wizard. We
	 	 * will create an operation and run it using wizard as execution context.
	 	 */
	 	public boolean performFinish() {

	 		IRunnableWithProgress op = new IRunnableWithProgress() {
	 			public void run(IProgressMonitor monitor)
	 					throws InvocationTargetException {
	 				try {
	 					doFinish(monitor);
	 				} catch (CoreException e) {
	 					throw new InvocationTargetException(e);
	 				} finally {
	 					monitor.done();
	 				}
	 			}
	 		};
	 		
	 		try {
	 			getContainer().run(true, false, op);
	 		} catch (InterruptedException e) {
	 			return false;
	 		} catch (InvocationTargetException e) {
	 			Throwable realException = e.getTargetException();
	 			MessageDialog.openError(getShell(), "Error",
	 					realException.getMessage());
	 			return false;
	 		}
	 		return true;
	 	}

	 	/**
	 	 * The worker method. It will find the container, create the file if missing
	 	 * or just replace its contents, and open the editor on the newly created
	 	 * file.
	 	 */
	 	@SuppressWarnings("unused")
	 	private void doFinish(IProgressMonitor monitor) throws CoreException {
	 		monitor.beginTask("Setting options", 1);
	 		selectedClassList = testChoosePage.getSelectedTests();
			System.out.println("RunCustomTestsWizard.doFinish()");
	 		monitor.done();
	 	}

	 	/**
	 	 * We will accept the selection in the workbench to see if we can initialize
	 	 * from it.
	 	 * 
	 	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
	 	 */
	 	public void init(IWorkbench workbench, IStructuredSelection selection) {
	 		this.selection = selection;
	 	}

		public List<AAtestClass> getSelectedClassList() {
			return selectedClassList;
		}

		public void setSelectedClassList(List<AAtestClass> selectedClassList) {
			this.selectedClassList = selectedClassList;
		}
	 	
	 	
}
